package com.aspire.aimini.config;

import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;
import io.swagger.v3.oas.models.servers.Server;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

/**
 * Swagger配置类
 *
 * @author wangguiyou
 * @date 2024-12-19
 * @description 配置Swagger API文档生成
 */
@Configuration
public class SwaggerConfig {

        @Value("${spring.application.name:AI图形管理系统}")
        private String applicationName;

        @Value("${server.port:8080}")
        private String serverPort;

        /**
         * 配置OpenAPI
         *
         * @return OpenAPI配置
         */
        @Bean
        public OpenAPI customOpenAPI() {
                return new OpenAPI()
                                .info(apiInfo())
                                .servers(List.of(
                                                new Server().url("http://localhost:" + serverPort)
                                                                .description("本地开发环境"),
                                                new Server().url("https://api.example.com").description("生产环境")))
                                .addSecurityItem(new SecurityRequirement().addList("Bearer Authentication"))
                                .components(new io.swagger.v3.oas.models.Components()
                                                .addSecuritySchemes("Bearer Authentication",
                                                                new SecurityScheme()
                                                                                .type(SecurityScheme.Type.HTTP)
                                                                                .scheme("bearer")
                                                                                .bearerFormat("JWT")
                                                                                .description("请输入JWT Token")));
        }

        /**
         * API信息
         *
         * @return API信息
         */
        private Info apiInfo() {
                return new Info()
                                .title(applicationName + " API文档")
                                .description("AI图形管理系统后端接口文档")
                                .version("1.0.0")
                                .contact(new Contact()
                                                .name("wangguiyou")
                                                .email("wangguiyou@example.com")
                                                .url("https://github.com/wangguiyou"))
                                .license(new License()
                                                .name("MIT License")
                                                .url("https://opensource.org/licenses/MIT"));
        }
}